package com.xindaoapp.happypet.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.Post;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.view.CircleImageView;

public class SearchAdapter extends MyBaseAdapter<Post> {

    private final Context mContext;

    public SearchAdapter(Context context, List<Post> mList, int resId) {
        super(context, mList, resId);
        this.mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent, Post t) {
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if (null == holder) {
            holder = new ViewHolder();
            holder.userIcon = (CircleImageView) convertView.findViewById(R.id.persion_icon);
            holder.title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.userName = (TextView) convertView.findViewById(R.id.persion_name);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.nums = (TextView) convertView.findViewById(R.id.nums);
            convertView.setTag(holder);
        }
        ImageLoader.getInstance().displayImage(t.getUserface(), holder.userIcon);
        holder.title.setText(t.getSubject());
        holder.userName.setText(t.getAuthor());

        String time = CommonUtil.formatDate();
        if (t.getAddtime().split(" ")[0].equals(time.split(" ")[0])) {// 说明是今天
            String today = t.getAddtime().split(" ")[1];
            holder.time.setText(today.substring(0, 5));
        } else {
            holder.time.setText(t.getAddtime().split(" ")[0].substring(5, 10));
        }

        // holder.time.setText(t.getAddtime());
        CharSequence text = mContext.getString(R.string.search_item_nums, t.replies, t.views);
        holder.nums.setText(text);
        return convertView;
    }

    class ViewHolder {
        CircleImageView userIcon;// 用户头像
        TextView title;// 标题
        TextView userName;// 用户名字
        TextView time;// 时间
        TextView nums;// 查看次数/留言次数
    }

}
